import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/foundation.dart';
import 'package:package_info_plus/package_info_plus.dart';

class AppInfo {
  static late String version;
  static late String buildNumber;
  static late String appName;
  static late String packageName;
  static late String systemVersion;
  static late AppPlatform platform;
  static late String userAgent;
  static late String brand;
  static late String model;
  static late String deviceId;
  static late bool isPhysicalDevice;
  static late String phoneModel;
  static Future<void> init() async {
    await _initPackageInfo();
    await _initDeviceInfo();
  }

  static Future<void> _initPackageInfo() async {
    try {
      PackageInfo packageInfo = await PackageInfo.fromPlatform();
      appName = packageInfo.appName;
      packageName = packageInfo.packageName;
      version = packageInfo.version;
      buildNumber = packageInfo.buildNumber;
    } catch (e) {
      print("Error initializing package info: $e");
    }
  }

  static Future<void> _initDeviceInfo() async {
    final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();

    switch (defaultTargetPlatform) {
      case TargetPlatform.android:
        await _initAndroidInfo(deviceInfoPlugin);
        break;
      case TargetPlatform.iOS:
        await _initIOSInfo(deviceInfoPlugin);
        break;
      case TargetPlatform.ohos:
        // final DeviceInfoOhosPlugin deviceInfoOhosPlugin = DeviceInfoOhosPlugin();
        // await _initOhosInfo(deviceInfoPlugin);
        break;
      default:
        break;
    }
  }

  static Future<void> _initAndroidInfo(DeviceInfoPlugin deviceInfoPlugin) async {
    try {
      platform = AppPlatform.android;
      userAgent = "Android.feifei.learn";
      final info = await deviceInfoPlugin.androidInfo;
      isPhysicalDevice = info.isPhysicalDevice;
      brand = info.brand;
      model = info.model;
      deviceId = info.id;
      systemVersion = info.version.release;
    } catch (e) {
      debugPrint("Error initializing Android info: $e");
    }
  }

  static Future<void> _initIOSInfo(DeviceInfoPlugin deviceInfoPlugin) async {
    try {
      platform = AppPlatform.iOS;
      userAgent = "ios.feifei.learn";
      final info = await deviceInfoPlugin.iosInfo;
      isPhysicalDevice = info.isPhysicalDevice;
      brand = "Apple";
      model = info.model;
      deviceId = info.identifierForVendor ?? "unknown";
      systemVersion = info.systemVersion;
    } catch (e) {
      debugPrint("Error initializing iOS info: $e");
    }
  }
  //
  // static Future<void> _initOhosInfo(DeviceInfoPlugin deviceInfoPlugin) async {
  //   OhosDeviceInfo info = await deviceInfoPlugin.ohosInfo;
  //   try {
  //     platform = AppPlatform.ohos;
  //     userAgent = "Harmony.volvo.car";
  //     brand = info.brand ?? 'unknown';
  //     systemVersion = info.osFullName ?? 'unknown';
  //     isPhysicalDevice = false;
  //     deviceId = info.productSeries ?? "unknown";
  //     model = info.marketName ?? 'unknown';
  //     phoneModel='${info.manufacture ?? 'unknown'}${info.productModel??'unknown'}';
  //   } catch (e) {
  //     debugPrint("Error initializing Harmony info: $e");
  //   }
  // }
}

enum AppPlatform {
  iOS,
  android,
  ohos,
  unknown,
}

// 注释的部分是 可以后期大家需要维护到属性里面
// static Map<String, dynamic> _readAndroidBuildData(AndroidDeviceInfo build) {
// return <String, dynamic>{
// 'version.securityPatch': build.version.securityPatch,
// 'version.sdkInt': build.version.sdkInt,
// 'version.release': build.version.release,
// 'version.previewSdkInt': build.version.previewSdkInt,
// 'version.incremental': build.version.incremental,
// 'version.codename': build.version.codename,
// 'version.baseOS': build.version.baseOS,
// 'board': build.board,
// 'bootloader': build.bootloader,
// 'brand': build.brand,
// 'device': build.device,
// 'display': build.display,
// 'fingerprint': build.fingerprint,
// 'hardware': build.hardware,
// 'host': build.host,
// 'id': build.id,
// 'manufacturer': build.manufacturer,
// 'model': build.model,
// 'product': build.product,
// 'supported32BitAbis': build.supported32BitAbis,
// 'supported64BitAbis': build.supported64BitAbis,
// 'supportedAbis': build.supportedAbis,
// 'tags': build.tags,
// 'type': build.type,
// 'isPhysicalDevice': build.isPhysicalDevice,
// 'systemFeatures': build.systemFeatures,
// 'serialNumber': build.serialNumber,
// };
// }
//
// static Map<String, dynamic> _readIosDeviceInfo(IosDeviceInfo data) {
// return <String, dynamic>{
// 'name': data.name,
// 'systemName': data.systemName,
// 'systemVersion': data.systemVersion,
// 'model': data.model,
// 'localizedModel': data.localizedModel,
// 'identifierForVendor': data.identifierForVendor,
// 'isPhysicalDevice': data.isPhysicalDevice,
// 'utsname.sysname:': data.utsname.sysname,
// 'utsname.nodename:': data.utsname.nodename,
// 'utsname.release:': data.utsname.release,
// 'utsname.version:': data.utsname.version,
// 'utsname.machine:': data.utsname.machine,
// };
// }
